var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var exec = require('child_process').exec;
var cmdStr = 'phantomjs index.js';
var download = require('./download');
var User = require('./_model');

 function route(key,equip) {
    return new Promise ((reslove,reject) => exec(cmdStr + ' ' + key + ' ' + equip, {
        cwd: 'D:/IFE/nuomi/node2'
    }, function (err, stdout, stderr) {
        var id 
        if (err) {
            console.error(`exec error: ${err}`);
        } else {
            var std = decodeURI(stdout);
            mongoose.connect("mongodb://localhost:27017/db", function (err) {
                if (err) {
                    console.log('连接失败');
                }
            });
            var obj1 = eval("("+std+")"); 
            var datalists = download.down(obj1.datalist);
            var doc1 = new User({
                code: obj1.code, 
                msg: obj1.msg, 
                word: obj1.word, 
                time: obj1.time, 
                equip:obj1.eq,
                dataList: datalists
            });
            
            doc1.save(function (err, doc) {
                console.log('保存成功:'); 
                reslove(doc._id)
            })
        }
       
    })
)
   

}

exports.route = route;